﻿module JulianDate

open System

let CalculateJulianDate (dt:System.DateTime) =
    let hours = System.Convert.ToDouble(dt.Hour)
    let minutes = System.Convert.ToDouble(dt.Minute)
    let seconds = System.Convert.ToDouble(dt.Second)

    let days = hours/24.00

    let decimalSeconds = seconds/60.00
    let decimalMinutes = (decimalSeconds + minutes)/60.00
    let decimalHours = (decimalMinutes + hours)/24.00

    let year = System.Convert.ToDouble(dt.Year)
    let month = System.Convert.ToDouble(dt.Month)
    let date = System.Convert.ToDouble(dt.Day)
    let decimalDate = date + decimalHours

    let y' =
        if month = 1.00 || month = 2.00 then year - 1.00
        else year

    let m' =
        if month = 1.00 || month = 2.00 then month + 12.00
        else month

    let A = y'/100.00
    let A' = System.Convert.ToInt32(A)
    let B = 2 - A' + (A'/4)

    let C =
        if y' < 0.00 then (365.25 * y') - 0.75
        else (365.25 * y')

    let D = (30.6001 * (m' + 1.00))

    let c' = System.Convert.ToInt32(C)
    let c'' = System.Convert.ToDouble(c')
    let d' = System.Convert.ToInt32(D)
    let d'' = System.Convert.ToDouble(d')
    let B' = System.Convert.ToDouble(B)
    let julianDate = B' + c'' + d'' + decimalDate + 1720994.5

    julianDate